/**
   @version 1.01 2001-07-17
   @author Cay Horstmann
*/
package org.wmine.algo;
import java.awt.*;
import java.awt.event.*;
import java.util.Enumeration;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.swing.*;
import javax.swing.tree.*;
public class TreeDisp
{  
   public static void main(String[] args)
   {  
	   Vector v1=new Vector();
	   Vector v2=new Vector();
	   v1.insertElementAt("1=senior,4=fair",0);
	   v1.insertElementAt("1=middle_aged",1);
	   v1.insertElementAt("1=youth,3=no",2);
	   v1.insertElementAt("1=senior,4=excellent",3);
	   v1.insertElementAt("1=youth,3=yes",4);
	   
	   
	   v2.insertElementAt("yes",0);
	   v2.insertElementAt("yes",1);
	   v2.insertElementAt("no",2);
	   v2.insertElementAt("no",3);
	   v2.insertElementAt("yes",4);
	   String [] columnNames={"RID","Age","income","Student","Credit Rating","buys Computer"};
	   JFrame frame = new SimpleTreeFrame(columnNames,v1,v2);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.show();
  }
}
class SimpleTreeFrame extends JFrame
{  
	int h;
	Container contentPane;
	String []attribs;
	Vector decision1;
   public SimpleTreeFrame(String[]attrib,Vector rules,Vector decision)
   {  
	   attribs=attrib;
	   decision1=decision;
      setTitle("SimpleTree");
      setSize(WIDTH, HEIGHT);
      contentPane = getContentPane();
      //classifyFirst()
      //===================
      int tokenKnt=0;	Vector tokens=new Vector();	int ind=0;
      for(int i=0;i<rules.size();i++)
      {
    		StringTokenizer st=new StringTokenizer(rules.elementAt(i).toString());
			//while(st.hasMoreElements())
			//{
				String s=st.nextToken(",");
				//StringTokenizer st1=new StringTokenizer(s);
				//int i=0;
				//while(st1.hasMoreElements())
				//{
					//String s1=st1.nextToken("=");
				int flg=0;
				for(int j=0;j<tokens.size();j++)
				{
					if(tokens.elementAt(j).toString().equals(s))
					{
						flg=1;
					}
				}
				if(flg==0)
					tokens.insertElementAt(s,tokenKnt++);
					//if(i==0)
					//{query.insertElementAt(s1,ind++);i=1;}
				//}
				
			//}
      }
    	  //====================
      basicBranches=tokens.size();
      buildTree(tokens);
    
      //call("1=Youth","2=Shree");
      //call("1=Youthi","2=Shree");
      for(h=0;h<rules.size();h++)
      {
    	  callVector(rules.elementAt(h).toString());
      }
      contentPane.add(new JScrollPane(tree));
   }
   public int basicBranches;
   public void buildTree(Vector v)
   {
	   	StringTokenizer st1=new StringTokenizer(v.elementAt(0).toString());
		//int i=0;
	   	//while(st1.hasMoreElements())
	   	//{
			String s1=st1.nextToken("=");
		//}
	   	  root  = new DefaultMutableTreeNode(attribs[Integer.parseInt(s1)]);
	   	  for(int k=0;k<v.size();k++)
	   	  {
	   		  country  = new DefaultMutableTreeNode(v.elementAt(k).toString());
	   		  root.add(country);
	   	  }  
	      tree = new JTree(root);
   }
   DefaultMutableTreeNode country;
   //////////
   
   
   public void callVector(String path)
   {
	   StringTokenizer st=new StringTokenizer(path);
	   Vector v=new Vector();
	   int ind=0;
	   while(st.hasMoreElements())
	   {
		   String s=st.nextToken(",");
		   v.insertElementAt(s,ind++);
	   }
	   int got=0;int pathInd=0;
	   Enumeration e=root.breadthFirstEnumeration();
	   System.out.println(e.nextElement().toString());
	   for(int i=0;i<basicBranches&&got==0;i++)
	   {   
		   	  
		   DefaultMutableTreeNode node=(DefaultMutableTreeNode)e.nextElement();
		   System.out.println(node+","+v.elementAt(0).toString());
		   //????????????????????
		   if(node.getUserObject().toString().equals(v.elementAt(0).toString()))
		   {
			   
			   pathInd=1;
			   //Enumeration e1=node.pathFromAncestorEnumeration(root);
			   Enumeration e1=node.children();
			   System.out.println(node.isLeaf()+" "+node.getUserObject());
			   int flg=0;
			   //while(e1.hasMoreElements())
			   //{
				 //  DefaultMutableTreeNode node2=(DefaultMutableTreeNode)e1.nextElement();
//				   System.out.println("before while");
	//		   }  
			   if(node.isLeaf()&&node.getUserObject().toString().equals(v.elementAt(pathInd-1).toString()))
			   {
				   while(pathInd<v.size())
				   {
					   System.out.println("adding");
					   node.add(new DefaultMutableTreeNode(v.elementAt(pathInd++)));
					   node=(DefaultMutableTreeNode)node.getChildAt(0);
					   System.out.println(node.isLeaf()+"+++++++++++++++++++");
				   }
				   node.add(new DefaultMutableTreeNode(decision1.elementAt(h)));
				   pathInd=0;
				   break;
			   }   	
			   else
				   callEnum(e1,v,pathInd);			   
			   
			   
			   
			   System.out.println("after while");
			 got=1;
		   }
		   //????????????????????
	   }
	}
   
 public void callEnum(Enumeration e1,Vector v,int pathInd)
 {
	  System.out.println("coming1");
	int flg=0;
	   while(e1.hasMoreElements())
	   {
		   System.out.println("coming2");
		   DefaultMutableTreeNode node1=(DefaultMutableTreeNode)e1.nextElement();
		   System.out.println(node1.getUserObject().toString()+" >>1");
		   System.out.println(v.elementAt(pathInd-1).toString()+">>2");
		   if(node1.isLeaf()&&node1.getUserObject().toString().equals(v.elementAt(pathInd-1).toString()))
		   {
			   while(pathInd<v.size())
			   {
				   System.out.println("adding");
				   node1.add(new DefaultMutableTreeNode(v.elementAt(pathInd++)));
				   node1=(DefaultMutableTreeNode)node1.getChildAt(0);
				   System.out.println(node1.isLeaf()+"+++++++++++++++++++");
				   flg=1;
			   }
			   node1.add(new DefaultMutableTreeNode(decision1.elementAt(h)));
			   pathInd=0;
			   break;
		   }
		   else
		   {
			   if((!node1.isLeaf())&&node1.getUserObject().toString().equals(v.elementAt(pathInd-1).toString()))
			   {
				   callEnum(node1.children(),v,pathInd);
			   }
			   else
				   if(!node1.getUserObject().toString().equals(v.elementAt(pathInd-1).toString()))
			   {
				   DefaultMutableTreeNode parent=(DefaultMutableTreeNode)node1.getParent();
				   parent.add(new DefaultMutableTreeNode(v.elementAt(pathInd++)));
				   
				   DefaultMutableTreeNode node2=(DefaultMutableTreeNode)parent.getChildAfter(node1);
				   //callEnum(node1.children(),v,pathInd);
				   while(pathInd<v.size())
				   {
					   //System.out.println("adding");
					   node2.add(new DefaultMutableTreeNode(v.elementAt(pathInd++)));
					   node2=(DefaultMutableTreeNode)node2.getChildAt(0);
					   System.out.println(node2.isLeaf()+"+++++++++++++++++++");
					   flg=1;
				   }
				   node2.add(new DefaultMutableTreeNode(decision1.elementAt(h)));
				   pathInd=0;
				   break;
			   }
		   }
		   //System.out.print(e1.nextElement()+">>>>>>>>>>>");
	   }
	   System.out.println("coming3");
	
 }
   
   
   
   
   
   
   
   
   
   //////////////
   
public void call(String s1,String s2)
{
	//Enumeration e=root.breadthFirstEnumeration();
	Enumeration e=root.breadthFirstEnumeration();
	while(e.hasMoreElements())
	{
		DefaultMutableTreeNode node=(DefaultMutableTreeNode)e.nextElement();
		Enumeration e1=node.pathFromAncestorEnumeration(root);
		while(e1.hasMoreElements())
		{
			System.out.print(e1.nextElement()+">>>>>>>>>>>");			
		}		
		System.out.println();
		//System.out.println(node.getUserObject()+" ..."+node.pathFromAncestorEnumeration());
		if(node.getUserObject().equals(s1))
		{
			//node.children()
			node.add(new DefaultMutableTreeNode(s2));
			System.out.println("yes");
			break;
			
		}
	}
	System.out.println("yes");
	
	}
JTree tree;
   private static final int WIDTH = 300;
   private static final int HEIGHT = 200;
   DefaultMutableTreeNode root;
}
